﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AntikCompta.Data;

namespace AntikCompta
{
    public partial class FraisWnd : Form
    {
        private Exercice mExercice;
        public FraisWnd(ref Exercice aExercice)
        {
            mExercice = aExercice;
            InitializeComponent();

            mExercice.DataChanged += new DataChangedHandler(mExercice_DataChanged);

            treeView1.SelectedNode = treeView1.Nodes[DateTime.Now.Month - 1];

            Text += " " + mExercice.Annee;

        }

        void mExercice_DataChanged()
        {
            RefreshView();
        }

        private int CurrentMois
        {
            get
            {
                if (treeView1.SelectedNode == null)
                    return -1;

                return Convert.ToInt32(treeView1.SelectedNode.Tag);
            }
        }

        private void RefreshView()
        {
            datas.Rows.Clear();
            int mois = CurrentMois;

            if (mois < 0)
                return;

            float somme = 0;
            foreach (CaisseElement elmt in mExercice.Frais)
            {
                if (elmt.Date.Month == mois)
                {
                    SetValues(elmt);
                    somme += elmt.Somme;
                }
            }
            lbl_tot.Text = "Total des frais pour le mois de " + treeView1.SelectedNode.Text + ": " + Math.Round(somme, 2);
        }

        private void SetValues(CaisseElement f)
        {
            int idxRow = datas.Rows.Add();
            datas.Rows[idxRow].Cells[0].Value = f.Date;
            int idxCol = 10;
            switch (f.Description.ToLower())
            {
                case ("essence"): idxCol = 1; break;
                case ("péage"): idxCol = 2; break;
                case ("hotel"): idxCol = 3; break;
                case ("restaurant"): idxCol = 4; break;
                case ("entretien voiture"): idxCol = 5; break;
                case ("sfr téléphone"): idxCol = 6; break;
                case ("publicité"): idxCol = 7; break;
                case ("place"): idxCol = 8; break;
                case ("loyer"): idxCol = 9; break;
                case ("divers"): idxCol = 10; break;
            }
            datas.Rows[idxRow].Cells[idxCol].Value = f.Somme;
            datas.Rows[idxRow].Cells[11].Value = f.Commentaires;
            datas.Rows[idxRow].Cells[12].Value = f.Reference;
        }
        

        private void bt_fermer_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FraisWnd_FormClosing(object sender, FormClosingEventArgs e)
        {
            mExercice.DataChanged -= new DataChangedHandler(mExercice_DataChanged);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NewFraisWnd wnd = new NewFraisWnd(ref mExercice, CurrentMois);
            wnd.ShowDialog();
        }

        private void bt_suppr_Click(object sender, EventArgs e)
        {
            if ((datas.SelectedCells == null) || (datas.SelectedCells.Count <= 0))
                return;

            DataGridViewRow row = datas.Rows[datas.SelectedCells[0].RowIndex];

            int reference = Convert.ToInt32(row.Cells[12].Value);
            if (reference != -1)
            {
                if (MessageBox.Show("Etes vous certain de vouloir supprimer l'élément du : " + row.Cells[0].Value.ToString() + "?", "Attention", MessageBoxButtons.OKCancel) != DialogResult.OK)
                    return;

                mExercice.SupprimerFrais(reference);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            RefreshView();
        }

        private void datas_SelectionChanged(object sender, EventArgs e)
        {
            txt_status.Text = "";
            if ((datas.SelectedCells == null) || (datas.SelectedCells.Count <= 0))
                return;

            float valeur = 0;
            foreach (DataGridViewCell cell in datas.SelectedCells)
            {
                if ((cell.ColumnIndex > 0) && (cell.Value != null) && (cell.ColumnIndex < 11))
                {
                    DataGridViewRow row = datas.Rows[cell.RowIndex];
                    if (row.Cells[12].Value != null)
                    {
                        int reference = Convert.ToInt32(row.Cells[12].Value);
                        if (reference != -1)
                        {
                            CaisseElement frais;
                            if (mExercice.GetFrais(reference, out frais))
                                valeur += frais.Somme;
                        }
                    }
                }
            }
            txt_status.Text = "somme = " + Math.Round(valeur,2) + " €";
        }

        private void bt_imprimer_Click(object sender, EventArgs e)
        {
            DataGridView dgp = Tools.Copy_DataGridView(datas);
            int idx = Tools.AddTotaux(dgp, 1, dgp.Columns.Count - 2);

            float total = 0;
            for (int i = 1; i < dgp.Columns.Count - 1; i++) // la derniere est pour les commentaires
            {
                total += Convert.ToSingle(dgp.Rows[idx].Cells[i].Value);
            }

            dgp.Rows[idx].Cells[11].Value = Math.Round(total,2);
            PrintDGV.Print_DataGridView(dgp, "Frais du mois " + treeView1.SelectedNode.Text + " " + mExercice.Annee);
        }
    }
}
